<template>
    <div class="list-page">
        <div class="list-page-header list-page-header-flex">
            <div class="flex-left">
                <el-button type="primary" @click="add">新增</el-button>
            </div>
            <div class="flex-right">
                <el-input v-model="query.name" class="search-input-class" placeholder="单位名称" />
                <el-button type="primary" @click="loadData">查询</el-button>
                <el-button @click="reset">重置</el-button>
            </div>
        </div>
        <div class="list-content">
            <el-table
                ref="datagrid"
                :data="table.data"
                border
                style="width: 100%; height: 100%"
                @row-click="handleClickTableRow"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" />
                <el-table-column prop="name" label="单位名称" width="300" show-overflow-tooltip />
                <el-table-column prop="typeName" label="单位类型" width="120" show-overflow-tooltip />
                <el-table-column prop="characterName" label="企业性质名称" width="120" show-overflow-tooltip />
                <el-table-column prop="branchOffice" label="所属办事处" width="160" show-overflow-tooltip />
                <el-table-column
                    prop="importanceDegree"
                    label="重要程度"
                    width="100"
                    :formatter="importanceFormatter"
                    show-overflow-tooltip
                />
                <el-table-column prop="telphone" label="单位联系电话" width="140" show-overflow-tooltip />
                <el-table-column prop="contact" label="联系人" width="100" show-overflow-tooltip />
                <el-table-column prop="post" label="联系人职务" width="120" show-overflow-tooltip />
                <el-table-column prop="phone" label="联系人电话" width="140" show-overflow-tooltip />
                <el-table-column fixed="right" label="操作" width="140">
                    <template #default="scope">
                        <el-button size="small" type="primary" @click="edit(scope.row)">编辑</el-button>
                        <el-popconfirm
                            title="正在进行敏感操作，是否继续？"
                            confirmButtonText="继续"
                            cancelButtonText="取消"
                            @confirm="del(scope.row)"
                        >
                            <template #reference>
                                <el-button type="danger" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="list-footer">
            <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="table.pagination.currentPage"
                :page-sizes="table.pagination.pageSizes"
                :page-size="table.pagination.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="table.pagination.total"
            ></el-pagination>
        </div>
        <!-- 新增编辑往来单位 -->
        <transition name="slide-fade">
            <div v-if="showInfoWindow" class="list-form-modal">
                <CompanyForm
                    :proOptions="proOptions"
                    :typeOptions="typeOptions"
                    :id="selectedId"
                    @close="closeInfoWindow"
                ></CompanyForm>
            </div>
        </transition>
    </div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElNotification } from 'element-plus'
import http from '@/http.js'
import CompanyForm from './form/CompanyForm.vue'

const datagrid = ref(null)

const query = reactive({
    name: ''
})

const table = reactive({
    data: [],
    selected: [],
    pagination: {
        currentPage: 1,
        pageSizes: [10, 20, 50, 100],
        pageSize: 10,
        total: 0
    }
})

const proOptions = ref([])

const typeOptions = ref([])

const showInfoWindow = ref(false)

const selectedId = ref('')

// 新增单位
const add = () => {
    selectedId.value = ''
    showInfoWindow.value = true
}

// 编辑
const edit = (row) => {
    selectedId.value = row.id
    showInfoWindow.value = true
}

const closeInfoWindow = () => {
    showInfoWindow.value = false
    loadData()
}

const handleSelectionChange = (val) => {
    table.selected = val
}

const handleSizeChange = (val) => {
    table.pagination.pageSize = val
    loadData()
}

const handleCurrentChange = (val) => {
    table.pagination.currentPage = val
    loadData()
}

const handleClickTableRow = (row) => {
    datagrid.value.clearSelection()
    datagrid.value.toggleRowSelection(row)
}

const loadData = () => {
    let params = {
        name: query.name,
        page: table.pagination.currentPage,
        pageSize: table.pagination.pageSize
    }
    http.fetchForm({
        url: '/pub/company/queryGrid',
        data: params
    }).then((res) => {
        table.data = res.data.grid
        table.pagination.total = res.data.total
    })
}

const importanceFormatter = (row, column, cellValue, index) => {
    let data = row[column.property]
    if (data == 0) {
        return '一般'
    } else {
        return '重要'
    }
}

const reset = () => {
    query.name = ''
    loadData()
}

const del = (row) => {
    http.fetchForm({
        url: '/pub/company/deleteById',
        method: 'post',
        data: {
            id: row.id
        }
    }).then((res) => {
        ElNotification({
            title: '提示',
            message: '删除成功',
            type: 'success'
        })
        loadData()
    })
}

const loadOptions = () => {
    http.fetchForm({
        url: '/sys/dictionary/queryByGroups',
        data: {
            groupCodes: 'P-002-002,P-002-001'
        }
    }).then((res) => {
        proOptions.value = res.data['P-002-001']
        typeOptions.value = res.data['P-002-002']
    })
}

// mounted
onMounted(() => {
    loadData()
    loadOptions()
})
</script>
<style scoped>
@import '@/assets/css/list-page.css';
</style>
